package com.aubrey.threadDemo.lock;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ConditionOperator {
	
	private LinkedList linkList = new LinkedList();
	
	private Lock lock = new ReentrantLock();
	
	private Condition notEmpty = lock.newCondition();
	
	private Condition notFull = lock.newCondition();
	
	private int maxCount = 10;
	
	public void put(String name) {
		lock.lock();
		if(linkList.size() == maxCount) {
			try {
				System.out.println("=====放满了..." +linkList.size());
				notFull.await();
				
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		notEmpty.signalAll();
		linkList.add(name);
		System.out.println("put：" + name);
		lock.unlock();
		
	}
	
	public void get() {
		lock.lock();
		if(linkList.size() == 0) {
			try {
				System.out.println("=====拿完了，已经没有了...");
				notEmpty.await();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		notFull.signalAll();
		System.out.println("get：" + linkList.removeFirst());
		lock.unlock();
	}
}
